#include"threadPool.h"
#include"logger.h"
#include"makeWorker.h"
#include"tcpInit.h"
#include"mysql.h"
#include"task.h"
#include"register_login.h"


#define WORKER_NUM 10
#define TASK_NUM 100
#define USER_FILE_PATH "/home/lijie/Linux/百度云C/baidu_cloud_v3/cloud-storage-3/user_file_content"

int main(int argc, char *argv[])
{
    //./sever ip port
    ARGS_CHECK(argc, 3);
    //创建线程池
    threadPool_t* pool = (threadPool_t*)calloc(1, sizeof(threadPool_t));
    ERROR_CHECK(pool, NULL, "calloc");
    //初始化线程池结构体
    threadPoolInit(pool, WORKER_NUM, TASK_NUM);
    //初始化日志
    log_init(LOG_INFO, "log.txt"); // 初始化日志系统，输出到 stderr

    //创建套接字
    int sockfd = 0;
    tcpInit(&sockfd, argv[1], argv[2]);
    LOG_INFO("tcpInit success\n");
    //创建工人线程
    workerInit(pool, WORKER_NUM);

    //创建epoll事件表
    int epfd = 0;
    epollCreate(&epfd, sockfd);
    LOG_INFO("epfd = %d\n", epfd);
    epollAdd(epfd, sockfd);

    //创建mysql连接池
    pool->mysql_pool = (MysqlPool_t*)calloc(10, sizeof(MysqlPool_t));
    mysql_pool_init(pool->mysql_pool, "localhost", "root", "Xujinda1996.", "baidu_disk");

    LOG_INFO("mysql_pool_init success\n");
    //进行监听
    int readyNum = 0;
    struct epoll_event readyEvents[1024];
    while(1){
        readyNum = epoll_wait(epfd, readyEvents, 1024, -1);
        LOG_INFO("epoll_wait readyNum = %d\n", readyNum);
        for(int i = 0; i < readyNum; i++){
            if(readyEvents[i].data.fd == sockfd){
                //有新的连接
                struct sockaddr_in client;
                socklen_t len = sizeof(client);
                int newfd = accept(sockfd, (struct sockaddr*)&client, &len);
                ERROR_CHECK(newfd, -1, "accept");
                LOG_INFO("accept new client %d\n", newfd);
                //注册或登陆，用户登录成功添加到哈希表
                log_Or_reg(newfd, pool->mysql_pool);
                //添加到epoll事件表
                epollAdd(epfd, newfd);
            }else{
                //有新任务数据到来，创建任务
                //接收命令
                train_t train;
                bzero(&train, sizeof(train));
                int ret = recv_n(readyEvents[i].data.fd, &train.train_len, sizeof(train.train_len));
                if (ret == 0){
                    //客户端断开连接
                    LOG_INFO("client %d disconnected\n", readyEvents[i].data.fd);
                    //从哈希表中删除
                    delete_session(readyEvents[i].data.fd);
                    //从epoll事件表中删除
                    epollDel(epfd, readyEvents[i].data.fd);
                    //关闭套接字
                    close(readyEvents[i].data.fd);
                    continue;
                }
                ERROR_CHECK(ret, -1, "recv_n");
                LOG_INFO("recv train_len = %d\n", train.train_len);
                ret = recv_n(readyEvents[i].data.fd, train.train_data, train.train_len);
                ERROR_CHECK(ret, -1, "recv_n");
                LOG_INFO("recv train_data = %s\n", train.train_data);
                char cmd[128] = {0};
                strcpy(cmd, train.train_data);
                LOG_INFO("cmd = %s\n", cmd);
                //加入任务
                taskInqueue(pool, cmd, readyEvents[i].data.fd);

            }
        }
    }






















    return 0;

}